package com.pic4493.comm;

import java.security.PublicKey;

import com.pic4493.app.R;
import com.pic4493.entities.EUser;
import com.ppcodes.imageloader.cache.disc.naming.Md5FileNameGenerator;
import com.ppcodes.imageloader.core.ImageLoader;
import com.ppcodes.imageloader.core.ImageLoaderConfiguration;
import com.ppcodes.imageloader.core.assist.QueueProcessingType;
import com.ppcodes.imageloader.core.download.URLConnectionImageDownloader;

import android.app.Application;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.pm.PackageManager;
import android.graphics.Bitmap;
import android.telephony.TelephonyManager;
import android.util.Log;

/**
 * Session全局静态变量,用于存储公共信息,生命周期同整个APPLICATION
 * 
 * @author Administrator
 * 
 */
public class Session extends Application
{
   private static Session instance = null;
   public final static String DEFAULT_USER_ID = "10000";

   public static synchronized Session getInstance()
   {
	  if (instance == null)
		 instance = new Session();
	  return instance;
   }

   private String mVersion;
   private String mImei;
   private String mPostUrl;
   private String mChannel;
   private String mVerify;
   private EUser mUser;

   public Bitmap picCache;
   public Bitmap picWallPaper;

   public String getVersion()
   {
	  return mVersion;
   }

   public String getImei()
   {
	  return mImei;
   }

   public String getPostUrl()
   {
	  return mPostUrl;
   }

   public String getChannel()
   {
	  return mChannel;
   }

   public String getVerify()
   {
	  return mVerify;
   }

   /**
    * 
    * @return
    */
   public EUser getUser()
   {
	  return mUser;
   }

   /**
    * 是否有新消息
    * 
    * @return
    */
   public boolean hasNews()
   {
	  if (mUser.getNewsCount() > 0)
	  {
		 return true;
	  }
	  else
	  {
		 return false;
	  }
   }

   /**
    * 用户消息数
    * 
    * @return
    */
   public int getNewsCount()
   {
	  return mUser.getNewsCount();
   }

   /**
    * 获取QQ的APPID
    * 
    * @return
    */
   public String getQQAppId()
   {
	  try
	  {
		 String str = getPackageManager().getApplicationInfo(getPackageName(), PackageManager.GET_META_DATA).metaData.getString("QQ_APPID");
		 return str;
	  }
	  catch (Exception e)
	  {
		 Log.d("ERROR", "Session_getQQAppId() " + e);
	  }
	  return null;
   }

   public String getUserId()
   {
	  try
	  {
		 if (mUser == null || mUser.getUserId() == null || mUser.getUserId().equals(""))
		 {
			return DEFAULT_USER_ID;
		 }
		 else
		 {
			return mUser.getUserId();
		 }
	  }
	  catch (Exception e)
	  {
		 Log.d("ERROR", "Session_getUserId() " + e);
		 return DEFAULT_USER_ID;
	  }
   }

   public void setUser(EUser user)
   {
	  try
	  {
		 this.mUser = user;
		 SharedPreferences sp = getSharedPreferences(PubConst.Setting.USER_INFO, Context.MODE_PRIVATE);
		 SharedPreferences.Editor editor = sp.edit();
		 editor.putString(PubConst.Setting.USERNAME, user.getUserName());
		 // editor.putString(PubConst.Setting.PASSWORD, user.getPassWord());
		 editor.putString(PubConst.Setting.NICKNAME, user.getNickName());
		 editor.putString(PubConst.Setting.AVATARS, user.getHeadImage());
		 editor.putString(PubConst.Setting.UID, user.getUserId());
		 editor.commit();
	  }
	  catch (Exception e)
	  {
		 Log.d("ERROR", "Session_setUser(user) " + e);
	  }
   }

   public void logout()
   {
	  setUser(null);
	  SharedPreferences.Editor editor = getSharedPreferences(PubConst.Setting.USER_INFO, Context.MODE_PRIVATE).edit();
	  editor.clear();
	  editor.commit();
   }

   @Override
   public void onCreate()
   {
	  super.onCreate();
	  instance = (Session) getApplicationContext();

	  try
	  {
		 // 初始化图片加载模块
		 ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(getApplicationContext()).threadPriority(Thread.NORM_PRIORITY - 1)// 设置下载模块线程优先级
			   .threadPoolSize(2)// 线程池大小
			   .memoryCacheSize(2 * 1024 * 1024) // 2 Mb 内存缓存大小
			   .denyCacheImageMultipleSizesInMemory().discCacheFileNameGenerator(new Md5FileNameGenerator()) // 图片以MD5命名
			   .imageDownloader(new URLConnectionImageDownloader(10 * 1000, 20 * 1000))// 设置下载器
			   .tasksProcessingOrder(QueueProcessingType.FIFO)// 设置下载队列模式
			   // .enableLogging()
			   .build();

		 ImageLoader.getInstance().init(config);

		 mVersion = String.valueOf(getPackageManager().getPackageInfo(getPackageName(), 0).versionCode);
		 mImei = ((TelephonyManager) getSystemService(TELEPHONY_SERVICE)).getDeviceId();
		 mPostUrl = getResources().getString(R.string.url_api);
		 mChannel = getPackageManager().getApplicationInfo(getPackageName(), PackageManager.GET_META_DATA).metaData.getString("CHANNEL");
		 mVerify = getPackageManager().getApplicationInfo(getPackageName(), PackageManager.GET_META_DATA).metaData.getString("VERIFY");
	  }
	  catch (Exception e)
	  {
		 Log.d("ERROR", "Session_onCreate() " + e);
	  }
   }

   public boolean isLogin()
   {
	  if (mUser != null && mUser.getUserId() != null && !mUser.getUserId().equals("") && !mUser.getUserId().equals(DEFAULT_USER_ID))
	  {
		 return true;
	  }
	  else
	  {
		 return false;
	  }
   }

   /**
    * 是不是从本地自动登录
    * @return
    */
   public boolean isAutoLogin()
   {
	  try
	  {
		 SharedPreferences sp = getSharedPreferences(PubConst.Setting.USER_INFO, Context.MODE_PRIVATE);
		 String uid = sp.getString(PubConst.Setting.UID, null);
		 if (uid != null)
		 {
			return true;
		 }
	  }
	  catch (Exception e)
	  {
		 Log.d("ERROR", "Session_isAutoLogin() " + e);
	  }
	  return false;
   }

   /**
    * 从本地登录
    */
   public void loginAuto()
   {
	  try
	  {
		 SharedPreferences sp = getSharedPreferences(PubConst.Setting.USER_INFO, Context.MODE_PRIVATE);
		 mUser = new EUser();	
		 mUser.setUserId(sp.getString(PubConst.Setting.UID,""));
		 mUser.setNickName(sp.getString(PubConst.Setting.NICKNAME,""));
		 mUser.setHeadImage(sp.getString(PubConst.Setting.AVATARS,""));
		 mUser.setPassWord(sp.getString(PubConst.Setting.PASSWORD,""));
		 mUser.setUserName(sp.getString(PubConst.Setting.USERNAME,""));
	  }
	  catch (Exception e)
	  {
		 Log.d("ERROR", "Session_loginAuto() " + e);
	  }

   }
}
